Root Zanli
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
o5t6x7pgljbm
/
public_html
/
admin
/
app
/
V2
/
Services
/
Filename :
AmazonProductAdvertisingApiService.php
back
Copy
<?php namespace App\V2\Services; use GuzzleHttp\Client; use Illuminate\Support\Facades\Log; class AmazonProductAdvertisingApiService { protected $client; protected $endpoint = 'https://webservices.amazon.com/paapi5/searchitems'; protected $host = 'webservices.amazon.com'; protected $contentType = 'application/json; charset=UTF-8'; protected $target = 'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems'; protected $userAgent = 'paapi-docs-curl/1.0.0'; protected $accessKey; protected $secretKey; protected $partnerTag; protected $region = 'us-east-1'; public function __construct() { $this->client = new Client(); $this->accessKey = config('services.amazon-paapi.key'); $this->secretKey = config('services.amazon-paapi.secret'); $this->partnerTag = config('services.amazon-paapi.partner-tag'); } public function searchProducts($keywords, $itemCount) { $timestamp = gmdate('Ymd\THis\Z'); $date = gmdate('Ymd'); $payload = [ 'Marketplace' => 'www.amazon.com', 'PartnerType' => 'Associates', 'PartnerTag' => $this->partnerTag, 'Keywords' => $keywords, 'SearchIndex' => 'All', 'ItemCount' => intval($itemCount), 'Resources' => ['Images.Primary.Large', 'ItemInfo.Title', 'Offers.Listings.Price'] ]; $payloadJson = json_encode($payload); $host = "webservices.amazon.com"; $path = "/paapi5/searchitems"; $awsv4 = new AwsV4 ($this->accessKey, $this->secretKey); $awsv4->setRegionName("us-east-1"); $awsv4->setServiceName("ProductAdvertisingAPI"); $awsv4->setPath ($path); $awsv4->setPayload ($payloadJson); $awsv4->setRequestMethod ("POST"); $awsv4->addHeader ('content-encoding', 'amz-1.0'); $awsv4->addHeader ('content-type', 'application/json; charset=utf-8'); $awsv4->addHeader ('host', $host); $awsv4->addHeader ('x-amz-target', 'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems'); $headers = $awsv4->getHeaders (); // $headers = [ // 'Content-Type' => $this->contentType, // 'X-Amz-Date' => $timestamp, // 'X-Amz-Target' => $this->target, // 'Content-Encoding' => 'amz-1.0', // 'User-Agent' => $this->userAgent, // 'Host' => $this->host, // ]; // $signedHeaders = 'content-encoding;host;x-amz-date;x-amz-target'; // $signature = $this->generateSignature($payloadJson, $timestamp, $date, $signedHeaders); // $headers['Authorization'] = sprintf( // 'AWS4-HMAC-SHA256 Credential=%s/%s/%s/ProductAdvertisingAPI/aws4_request, SignedHeaders=%s, Signature=%s', // $this->accessKey, // $date, // $this->region, // $signedHeaders, // $signature // ); try { Log::debug("Request Headers for Amazon: " . print_r($headers, true)); Log::debug("Request Payload for Amazon: " . print_r($payloadJson, true)); $response = $this->client->post($this->endpoint, [ 'headers' => $headers, 'body' => $payloadJson ]); Log::debug("Response from Amazon: " . $response->getBody()->getContents()); Log::debug("Response Headers from Amazon: " . print_r($response->getHeaders(), true)); return json_decode($response->getBody(), true); } catch (\Exception $e) { if ($e instanceof \GuzzleHttp\Exception\ClientException) { $response = $e->getResponse(); $responseBody = $response->getBody()->getContents(); $responseHeaders = $response->getHeaders(); Log::error('Client error: ' . $e->getMessage()); Log::error('Response Body: ' . $responseBody); Log::error('Response Headers: ' . print_r($responseHeaders, true)); return json_decode($responseBody, true); } else { Log::error('Error fetching products from Amazon: ' . $e->getMessage()); Log::error('Error Trace: ' . $e->getTraceAsString()); return null; } } } protected function generateSignature($payload, $timestamp, $date, $signedHeaders) { $canonicalRequest = "POST\n/paapi5/searchitems\n\nhost:{$this->host}\n\n{$signedHeaders}\n" . hash('sha256', $payload); $stringToSign = "AWS4-HMAC-SHA256\n{$timestamp}\n{$date}/{$this->region}/ProductAdvertisingAPI/aws4_request\n" . hash('sha256', $canonicalRequest); $signingKey = $this->getSignatureKey($this->secretKey, $date, $this->region, 'ProductAdvertisingAPI'); return hash_hmac('sha256', $stringToSign, $signingKey); } protected function getSignatureKey($key, $date, $region, $service) { $kSecret = "AWS4" . $key; $kDate = hash_hmac('sha256', $date, $kSecret, true); $kRegion = hash_hmac('sha256', $region, $kDate, true); $kService = hash_hmac('sha256', $service, $kRegion, true); $kSigning = hash_hmac('sha256', 'aws4_request', $kService, true); return $kSigning; } }